package com.zhihaoliang.baseframe.ui;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.zhihaoliang.baseframe.R;
import com.zhihaoliang.baseframe.databinding.ProductItemBinding;
import com.zhihaoliang.baseframe.model.Product;

import java.util.List;

/**
 * 创建日期：2020-02-26
 * 描述:产品列表的Adapter
 * 作者:支豪亮
 */
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private List<? extends Product> mProductList;

    private final ProductClickCallback mProductClickCallback;

    public ProductAdapter(@NonNull ProductClickCallback productClickCallback) {
        mProductClickCallback = productClickCallback;
        /*
         * Recylerview的item是 ImageView 和  TextView构成，当数据改变时，我们会调用 notifyDataSetChanged，
         * 这个时候列表会刷新，为了使 url 没变的 ImageView 不重新加载（图片会一闪），我们可以用
         */
        setHasStableIds(true);
    }

    public void setProductList(List<? extends Product> list) {
        if (mProductList == null) {
            mProductList = list;
            notifyItemChanged(0, list.size());
            return;
        }

        DiffUtil.DiffResult result = DiffUtil.calculateDiff(
                new DiffUtil.Callback() {
                    @Override
                    public int getOldListSize() {
                        return mProductList.size();
                    }

                    @Override
                    public int getNewListSize() {
                        return list.size();
                    }

                    @Override
                    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                        return mProductList.get(oldItemPosition).getId() == list.get(newItemPosition).getId();
                    }

                    @Override
                    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                        Product newProduct = list.get(newItemPosition);
                        Product oldProduct = mProductList.get(oldItemPosition);
                        return newProduct.getId() == oldProduct.getId() &&
                                TextUtils.equals(newProduct.getName(), oldProduct.getName()) &&
                                TextUtils.equals(newProduct.getDescription(), oldProduct.getDescription()) &&
                                newProduct.getPrice() == oldProduct.getPrice();
                    }
                }
        );
        mProductList = list;
        //提高刷新效率
        result.dispatchUpdatesTo(this);
    }


    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ProductItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.product_item, parent, false);
        binding.setCallback(mProductClickCallback);
        return new ProductViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        holder.binding.setProduct(mProductList.get(position));
        /*
          调用executePendingBindings意味着您实质上是在调用该绑定时立即强制框架执行它到目前为止在绑定上需要做的所有事情
         */
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return mProductList == null ? 0 : mProductList.size();
    }

    static class ProductViewHolder extends RecyclerView.ViewHolder {

        final ProductItemBinding binding;

        ProductViewHolder(@NonNull ProductItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
